package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.DetailReceipt;
import com.ruoyi.common.core.domain.entity.HeadReceipt;
import com.ruoyi.common.core.domain.entity.ReceiptFrom;
import com.ruoyi.system.mapper.DetailReceiptMapper;
import com.ruoyi.system.mapper.HeadReceiptMapper;
import com.ruoyi.system.mapper.InventoryMapper;
import com.ruoyi.system.mapper.SalesMapper;
import com.ruoyi.system.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 采购单 业务层处理
 *
 * @author kc
 */
@Service
public class SalesServiceImpl implements SalesService {

    @Autowired
    protected Validator validator;
    @Autowired
    private SalesMapper SOMapper;
    @Autowired
    private HeadReceiptMapper HRMapper;
    @Autowired
    private DetailReceiptMapper DRMapper;
    @Autowired
    private InventoryMapper ItMapper;

    /**
     * 销售头单查询
     */
    @Override
    public List<HeadReceipt> SalesHeadReceiptQuery(HeadReceipt bo) {
        return SOMapper.SalesHeadReceiptQuery(bo);
    }

    /**
     * 销售明细单查询
     */
    @Override
    public List<DetailReceipt> SalesDetailReceiptQuery(DetailReceipt bo) {
        return SOMapper.SalesDetailReceiptQuery(bo);
    }

    @Override
    public HeadReceipt selectSalesOrderById(String systematicReceipt) {
        return SOMapper.selectSalesOrderById(systematicReceipt);
    }


    /**
     * 更新退库单据明细
     */
    private void addDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        DRMapper.delDetailReceipt(details);
        DRMapper.addDetailReceipt(details);
        if (count == 0) {
            ItMapper.addInventory(details);
        } else {
            ItMapper.updateWarehousingInventory(details);
        }
    }

    /**
     * 更新出库单据明细
     */
    private void outboundDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        if (count == 0) {
            throw new RuntimeException("货品明细中有货品库存为0，货品需入库后才能出库！");
        } else {
            DRMapper.delDetailReceipt(details);
            DRMapper.addDetailReceipt(details);
            ItMapper.updateRetrievalInventory(details);
        }
    }

    /**
     * 保存销售出库单据信息
     */
    @Override
    public int saveSalesOutbound(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            DRMapper.addDetailReceipt(details);
            return HRMapper.addHeadReceipt(bo);
        } else {
            if (bo.getReceiptStatus() == 1) {
                DRMapper.delDetailReceipt(details);
                DRMapper.addDetailReceipt(details);
            } else if (bo.getReceiptStatus() == 2) {
                outboundDetails(details);
            }
            return HRMapper.updateHeadReceipt(bo);
        }
    }

    /**
     * 保存销售退库单据信息
     */
    @Override
    public int saveSalesRetreat(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            DRMapper.addDetailReceipt(details);
            return HRMapper.addHeadReceipt(bo);
        } else {
            if (bo.getReceiptStatus() == 1) {
                DRMapper.delDetailReceipt(details);
                DRMapper.addDetailReceipt(details);
            } else if (bo.getReceiptStatus() == 2) {
                addDetails(details);
            }
            return HRMapper.updateHeadReceipt(bo);
        }
    }


}
